package com.ruoyi.project.tugboat.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.project.tugboat.domain.vo.TMqttMsgDisplayVO;
import com.ruoyi.project.tugboat.task.MqttTask;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.tugboat.domain.TMqttMsg;
import com.ruoyi.project.tugboat.service.ITMqttMsgService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2023-07-20
 */
@RestController
@RequestMapping("/system/msg")
public class TMqttMsgController extends BaseController
{
    @Autowired
    private ITMqttMsgService tMqttMsgService;
    @Autowired
    private MqttTask mqttTask;

    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:msg:listByParams')")
    @GetMapping("/listByParams")
    // String tugboatId, String operId, String deviceId, String dataType, java.util.Date startTime, Date endTime
    public TableDataInfo listByParams(@RequestParam(value = "tugboatId", required = true) String tugboatId,
                                      @RequestParam(value = "operType", required = true) String operType,
                                      @RequestParam(value = "deviceId", required = false) String deviceId,
                                      @RequestParam(value = "msgType", required = false) String msgType,
                                      @RequestParam(value = "startTime", required = false) Date startTime,
                                      @RequestParam(value = "endTime", required = false) Date endTime) 
    {
        startPage();
        List<TMqttMsgDisplayVO> list = tMqttMsgService.selectTMqttMsgListByParams(tugboatId, operType, deviceId, msgType, startTime, endTime);
        return getDataTable(list);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:msg:list')")
    @GetMapping("/list")
    public TableDataInfo list(TMqttMsg tMqttMsg)
    {
        startPage();
        List<TMqttMsg> list = tMqttMsgService.selectTMqttMsgList(tMqttMsg);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:msg:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TMqttMsg tMqttMsg)
    {
        List<TMqttMsg> list = tMqttMsgService.selectTMqttMsgList(tMqttMsg);
        ExcelUtil<TMqttMsg> util = new ExcelUtil<TMqttMsg>(TMqttMsg.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:msg:query')")
    @GetMapping(value = "/{msgid}")
    public AjaxResult getInfo(@PathVariable("msgid") String msgid)
    {
        return success(tMqttMsgService.selectTMqttMsgByMsgid(msgid));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:msg:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TMqttMsg tMqttMsg)
    {
        return toAjax(tMqttMsgService.insertTMqttMsg(tMqttMsg));
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:msg:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TMqttMsg tMqttMsg)
    {
        return toAjax(tMqttMsgService.updateTMqttMsg(tMqttMsg));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:msg:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{msgids}")
    public AjaxResult remove(@PathVariable String[] msgids)
    {
        return toAjax(tMqttMsgService.deleteTMqttMsgByMsgids(msgids));
    }

    /**
     * 手动转换mqtt报文接口函数
     * @return
     */
    @GetMapping("/manualConvert")
    public String convert(){
        mqttTask.autoConvertMqttMsg();
        return "转换成功";
    }
}
